package com.wish.base.service.impl;

import com.wish.base.dto.BusinessDto;
import com.wish.base.dto.SaveHaiLianDealPerDayRequest;
import com.wish.base.entity.CwHailianDealPerDay;
import com.wish.base.mapper.BusinessMapper;
import com.wish.base.mapper.CwDealPerDayMapper;
import com.wish.base.mapper.CwHailianDealPerDayMapper;
import com.wish.base.service.ICwHailianDealPerDayService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 海链财务报表(批处理对线上收入每天分类汇总统计一条记录，目前只有线上收入) 服务实现类
 * </p>
 *
 * @author group02
 * @since 2022-05-23
 */
@Service
@Slf4j
public class CwHailianDealPerDayServiceImpl extends ServiceImpl<CwHailianDealPerDayMapper, CwHailianDealPerDay> implements ICwHailianDealPerDayService {

    @Autowired
    private BusinessMapper businessDao;

    @Autowired
    private CwHailianDealPerDayMapper hailianDealPerDayMapper;

    @Autowired
    private CwDealPerDayMapper cwDealPerDayMapper;

    @Override
    @Transactional
    public void saveDealPerDay(Date yesterday) {
        log.info("saveDealPerDay处理   Start...");
        List<BusinessDto> allBussiness = businessDao.getAllBussiness();
        if (allBussiness == null) {
            return;
        }
        SaveHaiLianDealPerDayRequest saveDealPerDayRequest = new SaveHaiLianDealPerDayRequest();
        saveDealPerDayRequest.setDealDate(yesterday);// 统计日期
        for (BusinessDto businessDto : allBussiness) {
            saveDealPerDayRequest.setBusinessId(businessDto.getId().longValue());// 商户ID
            saveDealPerDayRequest.setBusinessName(businessDto.getName());// 商户名称
            // ①定时将商户指定数据交易日期的线上线下的各项费用收入/支出汇总到t_cw_deal_per_day表
            // 商户收入
            cwDealPerDayMapper.saveDealPerDayIncome(saveDealPerDayRequest);
            // 商户支出
            cwDealPerDayMapper.saveDealPerDayOutcome(saveDealPerDayRequest);

            // ②定时将商户指定数据交易日期的线上各项费用收入汇总到t_cw_hailian_deal_per_day表
            // 维数公寓收入
            hailianDealPerDayMapper.saveHaiLianIncome(saveDealPerDayRequest);
            // 维数公寓支出(不统计)
        }
        log.info("saveDealPerDay处理   End...");
    }
}
